//
//  UIController.m
//  SMP
//
//  Created by Shappy Say on 5/2/13.
//  Copyright (c) 2013 HSBC. All rights reserved.
//

#import "UIController.h"
#import "LoadingView.h"
#import "SpinnerView.h"
#import "PublicHomeClientPackProgressMaskView.h"

static SpinnerView *loadingView;
static PublicHomeClientPackProgressMaskView *processLoadingView;
static STAAlertView *staAlertView;

@implementation UIController

+(void)showLoading:(UIView *)view{
    NSLog(@"showLoading");
    if(!loadingView){
        CGRect rect = view.bounds;
        if(rect.size.width == 0 || rect.size.height==0)
            rect = CGRectMake(0, 0, 768, 1024);
        loadingView = [[SpinnerView alloc] initWithFrame:rect LargeSize:YES AlignToCenter:NO];
        
    }
    @try{
        [view addSubview:loadingView];
        [view bringSubviewToFront:loadingView];
        [loadingView startAnimating];
        
        view.userInteractionEnabled = NO;
    }@catch (NSException * e) {
        NSLog(@"Exception(showLoading): *********************************\n %@", [e description]);
    }
    
}

+(void)hideLoading{
    
    @try{
        if(!processLoadingView)
            [loadingView superview].userInteractionEnabled = YES;
        if(loadingView){
            NSLog(@"UIController.hideLoading");
            [loadingView stopAnimating];
            [loadingView removeFromSuperview];
            [loadingView release];
            loadingView = nil;
        }
       
    }@catch (NSException * e) {
        NSLog(@"Exception(hideLoading): *********************************\n %@", [e description]);
    }
}

+(void)showProgress:(float)percentage message:(NSString *)message inView:(UIView *)view{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [UIController hideLoading];
    if(window){
        @try{
            if(!processLoadingView){
                NSBundle *bundle = [NSBundle mainBundle];
                NSArray *topObjects = [bundle loadNibNamed:@"PublicHomeClientPackProgressMaskView" owner:nil options:nil];
                
                if([topObjects count]==0){
                    return;
                }
                processLoadingView = [topObjects objectAtIndex:0];
                [processLoadingView initialize];
            }
            
            [view addSubview:processLoadingView];
            [processLoadingView setProgress:percentage tip:message];
            CGPoint pt = window.rootViewController.view.center;
            processLoadingView.center = pt;// CGPointMake(pt.x, pt.y*4.0/3);
            
            window.userInteractionEnabled = NO;
        }@catch (NSException * e) {
            NSLog(@"Exception(showProgress): *********************************\n %@", [e description]);
        }
    }
}

+(void)hideProgress{
    NSLog(@"UIController.hideProgress");
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if(window){
        @try{
            if(processLoadingView){
                [processLoadingView removeFromSuperview];
                processLoadingView = nil;
            }
            window.userInteractionEnabled = YES;
            NSLog(@"Finish UIController.hideProgress");
        }@catch (NSException * e) {
            NSLog(@"Exception(hideProgress): *********************************\n %@", [e description]);
        }
    }
}

+ (void)showAlertWithTitle:(NSString *)title message:(NSString *)message cancelButton:(NSString *)cancelButtonTitle okButton:(NSString *)okButtonTitle inView:(UIView *)view delegate:(id<STAAlertViewDelegate>)delegate {
    NSLog(@"showLoading");
    if(!staAlertView){
        CGRect rect = view.bounds;
        if(rect.size.width == 0 || rect.size.height==0)
            rect = CGRectMake(0, 0, 768, 1024);
        NSBundle *bundle = [NSBundle mainBundle];
        NSArray *topObjects = [bundle loadNibNamed:@"STAAlertView" owner:nil options:nil];
        if([topObjects count]==0){
            return;
        }
        staAlertView = [topObjects objectAtIndex:0];
        [staAlertView settitle:title message:message cancelButton:cancelButtonTitle okButton:okButtonTitle delegate:delegate];
    }
    @try{
        [view addSubview:staAlertView];
        [view bringSubviewToFront:staAlertView];
        
    }@catch (NSException * e) {
        NSLog(@"Exception(showLoading): *********************************\n %@", [e description]);
    }
}

+ (void)showDownloadAlertWithTitle:(NSString *)title cancelButton:(NSString *)cancelButtonTitle inView:(UIView *)view delegate:(id<STAAlertViewDelegate>)delegate {
    NSLog(@"showLoading");
    if(!staAlertView){
        CGRect rect = view.bounds;
        if(rect.size.width == 0 || rect.size.height==0)
            rect = CGRectMake(0, 0, 768, 1024);
        NSBundle *bundle = [NSBundle mainBundle];
        NSArray *topObjects = [bundle loadNibNamed:@"STAAlertView" owner:nil options:nil];
        if([topObjects count]==0){
            return;
        }
        staAlertView = [topObjects objectAtIndex:0];
        [staAlertView startDownloadingWithTitle:title cancelButton:cancelButtonTitle delegate:delegate];
    }
    @try{
        [view addSubview:staAlertView];
        [view bringSubviewToFront:staAlertView];
        
    }@catch (NSException * e) {
        NSLog(@"Exception(showLoading): *********************************\n %@", [e description]);
    }
}

@end
